<?php

class Model_Admin extends PhalApi_Model_NotORM {

    public function getByUserName($username) {
        return $this->getORM()
            ->select('*')
            ->where('username = ?', $username)
            ->fetch();
    }

    public function updatelastlogintime($username) {
        return $this->getORM()
            ->where('username = ?', $username)
            ->update([
                'lastlogintime' => date("Y-m-d H:i:s")
            ]);
    }

    public function getByUserId($userId) {
        return $this->getORM()
            ->select('*')
            ->where('id = ?', $userId)
            ->fetch();
    }

    public function getlist($where=[]) {

        return $this->getORM()
            ->where($where)
            ->select('id as userid,username,nickname,email,rules,lastlogintime')
            ->fetchAll();

    }

    public function add($data = []) {
        if (count($data) == 0) return false;
        return $this->getORM()
            ->insert($data);
    }

    public function update($data = [], $userid = null) {

        if (count($data) == 0) return false;
        if (!$userid) return false;

        return $this->getORM()
            ->where('id', $userid)
            ->update($data);
    }

    public function deleteByUserId($userid) {
        if (!$userid) return false;
        return $this->getORM()
            ->where('id', $userid)
            ->delete();

    }

    protected function getTableName($id) {
        return 'admin';
    }
}
